package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;

public class ScheduleController implements Initializable {

/*
========================================================设置fxml里边展示的元素————开始===============================================
 */
    @FXML
    private Button refreshTimetable;

    @FXML
    private Button searchRommTable;

    @FXML
    private Button SearchTecherTable;

    @FXML
    private Button ClassNote;

    @FXML
    private TableView<CouseList> schedule;

    @FXML
    private TableColumn<CouseList, String> Monday;

    @FXML
    private TableColumn<CouseList, String> Tuesday;

    @FXML
    private TableColumn<CouseList, String> Wednesday;

    @FXML
    private TableColumn<CouseList, String> Thursday;

    @FXML
    private TableColumn<CouseList, String> Friday;

    @FXML
    private TableColumn<CouseList, String> Saturday;

    @FXML
    private TableColumn<CouseList, String> Sunday;
    @FXML
    private Label t1;



    /*
    ========================================================设置fxml里边展示的元素————结束===============================================
     */


/*
============================================================功能代码————开始=============================================================
 */
    String xuehao; //学生的学号
    ObservableList<CouseList> list= FXCollections.observableArrayList();//用于一条条展示课表的列表



    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {  //整个page的初始化，可以在这里进行初始的设置，会默认执行

        t1.setVisible(false);

        //得到学生学号
        try {
            Scanner input=new Scanner(new File("src/res/info.txt"));
            while (input.hasNext())
            {
                xuehao=input.next();
            }
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //得到7天学生的每一天的课的信息
        String[] MonCou= MysqlConn.getScheduleData(xuehao,"1");
        String[] TueCon= MysqlConn.getScheduleData(xuehao,"2");
        String[] WedCou= MysqlConn.getScheduleData(xuehao,"3");
        String[] ThurCou= MysqlConn.getScheduleData(xuehao,"4");
        String[] FriCou= MysqlConn.getScheduleData(xuehao,"5");
        String[] SatCou= MysqlConn.getScheduleData(xuehao,"6");
        String[] Suncou= MysqlConn.getScheduleData(xuehao,"7");

        //将信息传入list。因为javafx中tableview是按行来呈现，结合川大课表的特点，我设置了一个长度为10的数组，每个数组代表一小节的课
        //将每一天的第0到10节课一次传入list
      for(int i=0;i<10;i++)
      {
          list.add(new CouseList(MonCou[i],TueCon[i],WedCou[i],ThurCou[i],FriCou[i],SatCou[i],Suncou[i]));
      }


        //简单解释下tableview的工作原理（可能有偏差）
        //在这个地方把表格的每一个column的名字设置到对应的名字，由于存在CourseList类，程序会到CourseList类中按照这里设定的名字寻找get函数，得到要打印的值
        Monday.setCellValueFactory(new PropertyValueFactory<>("MonC"));
        Tuesday.setCellValueFactory(new PropertyValueFactory<>("TueC"));
        Wednesday.setCellValueFactory(new PropertyValueFactory<>("WedC"));
        Thursday.setCellValueFactory(new PropertyValueFactory<>("ThurC"));
        Friday.setCellValueFactory(new PropertyValueFactory<>("FriC"));
        Saturday.setCellValueFactory(new PropertyValueFactory<>("SatC"));
        Sunday.setCellValueFactory(new PropertyValueFactory<>("SunC"));

        schedule.setItems(list);
    }

    /*
    =================================================================功能代码——————结束=================================================
     */
        /*
=================================================================以下是界面转换====================================================================
     */
    @FXML
    void SwitchToNotePage(ActionEvent event) { //按下按钮后转到课堂笔记页面
        SceneManager.SwitchToNotePage(event);
    }

    @FXML
    void SwitchToSearchRoomTtblePage(ActionEvent event) { //按下按钮后转到查询教室课表界面
        SceneManager.SwitchToSearchRoomTtblePage(event);
    }

    @FXML
    void SwitchToSearchTeacherTtblePage(ActionEvent event) { //按下按钮后转到查询教师课表界面
        SceneManager.SwitchToSearchTeacherTtblePage(event);
    }

}
